package com.wnm.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnm.pojo.Goods;
import com.wnm.pojo.PageBean;
import com.wnm.service.GoodsService;
import com.wnm.service.impl.GoodsServiceImpl;

@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {
	private GoodsService goodsService = new GoodsServiceImpl();

	public void findAllGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String currentPage = request.getParameter("currentPage");
		PageBean<Goods> pageBean = goodsService.findAllGoods(Integer.parseInt(currentPage));
		List<Goods> list = pageBean.getList();
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("/pages/moreGoodsList.jsp").forward(request, response);
	}

	public void findNewGoods(HttpServletRequest request, HttpServletResponse response) {
		List<Goods> newGoods = goodsService.findNewGoods();
		ObjectMapper om = new ObjectMapper();
		try {
			om.writeValue(response.getWriter(), newGoods);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void findHotGoods(HttpServletRequest request, HttpServletResponse response) {
		List<Goods> hotGoods = goodsService.findHotGoods();
		ObjectMapper om = new ObjectMapper();
		try {
			om.writeValue(response.getWriter(), hotGoods);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void findGoodsBySalesnums(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Goods> saleGoods = goodsService.findGoodsBySalesnums();
		ObjectMapper om = new ObjectMapper();
		om.writeValue(response.getWriter(), saleGoods);

	}

	public void findPageByCid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cid = request.getParameter("cid");
		String currentPage = request.getParameter("currentPage");
		PageBean<Goods> pageBean = goodsService.findPageByCid(Integer.parseInt(cid), Integer.parseInt(currentPage));
		List<Goods> list = pageBean.getList();
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("/pages/goodsList.jsp").forward(request, response);
	}

	public void showListByCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String condition = request.getParameter("condition");
		String currentPage = request.getParameter("currentPage");
		PageBean<Goods> pageBean = goodsService.findPageByCondition(condition, Integer.parseInt(currentPage));
		List<Goods> list = pageBean.getList();
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("/pages/goodsList2.jsp").forward(request, response);
	}

	public void goodsDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int gid = Integer.parseInt(request.getParameter("gid"));
		Goods goods = goodsService.goodsDetail(gid);
		request.setAttribute("goods", goods);
		request.getRequestDispatcher("/pages/goods_details.jsp").forward(request, response);
	}
}
